/**
 * Visual Blocks Language
 *
 * Copyright 2012 Google Inc.
 * http://blockly.googlecode.com/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @fileoverview Generating Python for control blocks.
 * @author gasolin@gmail.com  (Fred Lin)
 */
'use strict';

goog.provide('Blockly.Python.loops');

goog.require('Blockly.Python');

Blockly.Python.control_wait = function() {
    //wait time
  // Blockly.Python.definitions_['import_time']="import time";
  var delay_time = Blockly.Python.valueToCode(this, 'DURATION', Blockly.Python.ORDER_UNARY_SIGN) || '1';
  var code = 'await self.wait([' + delay_time + '])\n';
   // var branch = Blockly.Python.addLoopTrap(code, this.id) ||
   //    Blockly.Python.LOOP_PASS;
  return code;

};

Blockly.Python.control_program_stop = function() {
    //停止程序
  Blockly.Python.definitions_['import_sys']="import sys";
  var code = 'sys.exit(0)\n';
  var branch = Blockly.Python.addLoopTrap(code, this.id) ||
      Blockly.Python.LOOP_PASS;
  return code;

};

Blockly.Python.operator_lt = function() {
  var argument0 = Blockly.Python.valueToCode(this, 'OPERAND1',Blockly.Python.ORDER_RELATIONAL) || '0';
  var argument1 = Blockly.Python.valueToCode(this, 'OPERAND2',Blockly.Python.ORDER_RELATIONAL) || '1';
  var code = argument0+'<'+argument1;
  return [code,Blockly.Python.ORDER_RELATIONAL];
};

Blockly.Python.operator_equals = function() {
  var argument0 = Blockly.Python.valueToCode(this, 'OPERAND1',Blockly.Python.ORDER_RELATIONAL) || '0';
  var argument1 = Blockly.Python.valueToCode(this, 'OPERAND2',Blockly.Python.ORDER_RELATIONAL) || '1';
  var code = argument0+'=='+argument1;
  return [code,Blockly.Python.ORDER_RELATIONAL];
};

Blockly.Python.operator_gt = function() {
  var argument0 = Blockly.Python.valueToCode(this, 'OPERAND1',Blockly.Python.ORDER_RELATIONAL) || '0';
  var argument1 = Blockly.Python.valueToCode(this, 'OPERAND2',Blockly.Python.ORDER_RELATIONAL) || '1';
  var code = argument0+'>'+argument1;
  return [code,Blockly.Python.ORDER_RELATIONAL];
};

Blockly.Python.operator_and = function() {
  var argument0 = Blockly.Python.valueToCode(this, 'OPERAND1',Blockly.Python.ORDER_LOGICAL_AND) || 'False';
  var argument1 = Blockly.Python.valueToCode(this, 'OPERAND2',Blockly.Python.ORDER_LOGICAL_AND) || 'Flase';
  var code = argument0+' and '+argument1;
  return [code,Blockly.Python.ORDER_LOGICAL_AND];
};

Blockly.Python.operator_or = function() {
  var argument0 = Blockly.Python.valueToCode(this, 'OPERAND1',Blockly.Python.ORDER_LOGICAL_OR) || 'False';
  var argument1 = Blockly.Python.valueToCode(this, 'OPERAND2',Blockly.Python.ORDER_LOGICAL_OR) || 'False';
  var code = argument0+' or '+argument1;
  return [code,Blockly.Python.ORDER_LOGICAL_OR];
};

Blockly.Python.operator_not = function() {
  var argument0 = Blockly.Python.valueToCode(this, 'OPERAND',Blockly.Python.ORDER_LOGICAL_NOT) || 'False';
  var code = 'not '+argument0;
  return [code,Blockly.Python.ORDER_LOGICAL_NOT];
};
